const fsPromise = require('fs/promises')
const fs = require('fs')
const fs2 = require('dk-fs')
const path = require('path')
// const marked = require('marked')
require('highlight.js')
const md = require('markdown-it')({
    html: true,
    linkify: false,
})
.use(require('markdown-it-highlight-lines'))
// .use(require('markdown-it-highlightjs/core'), { hljs: require('highlight.js/lib/core') })
.use(require('markdown-it-named-headings'))
const ejs = require('ejs')
const defaultConfig = require('./defaultConfig')

/**
 * 读取文件内容
 */
async function getFileContent(src) {
    const content = await fsPromise.readFile(src)
    return content.toString()
}

/**
 * 生成文件
 */
async function geneFile(filePath, content, replaced = false) {
    await fsPromise.writeFile(filePath, content)
    return true
}

/**
 * 将 md 内容转换为 html 内容
 */
function mdToHtml(mdStr) {
    // const html = marked(mdStr)
    const html = md.render(mdStr)
    return html
}

async function parseDirectory() {
    const rootPath = process.cwd()
    const fileList = await fsPromise.readdir(rootPath)
    const mdFileList = fileList.filter(file => path.extname(file) === '.md')

    const mdFilePath = mdFileList.map(file => {
        const fileName = path.parse(file).name
        return {
            title: fileName,
            srcPath: path.join(rootPath, file),
            destPath: path.join(rootPath, defaultConfig.outputPath, fileName + '.html'),
        }
    })

    return mdFilePath
}

async function buildOutputDir() {
    const outputDirPath = path.join(process.cwd(), defaultConfig.outputPath)
    if (fs.existsSync(outputDirPath)) {
        await fsPromise.rmdir(outputDirPath, { recursive: true })
    }
    await fsPromise.mkdir(outputDirPath)
}

async function copyAssetsDir () {
    const srcPath = path.resolve(__dirname, '../assets')
    const destPath = path.join(process.cwd(), defaultConfig.outputPath)
    fs2.copyDirSelf(srcPath, destPath)
}

async function geneIndexHtml(fileListPath) {
    // 获取 baseHtml
    const indexHtmlPath = path.resolve(__dirname, './index.html')
    const indexHtmlStr = await getFileContent(indexHtmlPath)

    const content = ejs.render(indexHtmlStr, { fileListPath})
    const destPath = path.join(process.cwd(), defaultConfig.outputPath, 'index.html')
    await geneFile(destPath, content)
}

async function main() {
    // 生成 build 目录
    await buildOutputDir()

    // 复制静态资源
    await copyAssetsDir()

    // 获取当前目录下的 md 文件
    const filePathList = await parseDirectory()

    // 生成首页
    await geneIndexHtml(filePathList)

    // 获取 baseHtml
    const baseHtmlPath = path.resolve(__dirname, './base.html')
    const baseHtmlStr = await getFileContent(baseHtmlPath)

    // 将 md 文件生成 html 文件
    filePathList.forEach(async file => {
        const mdStr = await getFileContent(file.srcPath)
        const htmlStr = mdToHtml(mdStr)

        const renderOptions = {
            __DOC_NAME__: file.title,
            __MARKDOWN_CONTENT__: htmlStr,
        }

        const content = ejs.render(baseHtmlStr, renderOptions)
        await geneFile(file.destPath, content)
    })
}

module.exports = main